package polymonkey.time;

//import processing.core.*;


/**
 *StopWatch is used to time events. There are 2 concepts to understand with the StopWatch. The current total and the Running Total. Both are begun with start(), it takes a copy of the current system time in Millis. While the watch is running the current total can be reset with reset(). This is used in a lap timing situation. However both the current total and the Running Total are stopped with stop(). It subtracts the current millis time from the stored time.
 *The result can be reported in milliseconds (as an int) or as seconds (as a float). Reports from the current total and the Running Total can only be returned after the watch has started.
 * 
 * @author Matt Ditton 
 * @version 1 
 */

public class StopWatch{

  private long t1 = 0;
  private long t2 = 0;
  private long runningTotal = 0;  
  
  private int run = 0;

  /**
   * StopWatch constructor
   * @param autoStart true automatically starts the watch. If you use StopWatch(false) then you'll have to use start()
   */
  public StopWatch(boolean autoStart)
  {
	  if(autoStart){
		  start();
	  }
  }  
  
  /**
   * Starts the StopWatch.
   */
  public void start(){
	  run = 1;
	  
	  t1 = System.currentTimeMillis();
	  
	  runningTotal = t1;
  }

  /**
   * Stops the StopWatch. Also stops the Running Total.
   */
  public void stop(){
	  t2 = System.currentTimeMillis() - t1;
	  
	  runningTotal = System.currentTimeMillis() - runningTotal;
	  
	  run = 2;
  }
  
  /**
   * Sets the StopWatch back to zero. However the running total of time is NOT reset. This method is useful in a lap time situation. 
   */
  // request from subpixel
  public void reset(){
	  t1 = System.currentTimeMillis();
	  t2 = 0;
  }
  
  /**
   * @return Returns the int value of the result. This is the number of milliseconds that the StopWatch counted. If you get the result before running the watch it'll print an error and return -1.
   */
  public int getResultMillis()
  {
	  switch (run){
	  
	  case 0:
		  //PApplet.print("StopWatch hasn't started yet");
		  return 0;
	  case 1:
		  return (int)(System.currentTimeMillis() - t1);
	  case 2:
		  return (int)t2;
	  }
	  
	  return -1; 
  }
  
  /**
   * @return Returns the seconds that the StopWatch counted as a float. If you get the result before running the watch it'll print an error and return -1.
   */
  public float getResultSeconds()
  {  
	  switch (run){
	  
	  case 0:
		  //PApplet.print("StopWatch hasn't started yet");
		  return 0;
	  case 1:
		  return (float)(System.currentTimeMillis() - t1) * 0.001f;
	  case 2:
		  return (float) t2 * 0.001f;
	  }
	  
	  return -1;
  }
  
	/**
	 * @return The Running Total of time in milliseconds as an int. The Running Total of time can only be reset with the start(). If you get the result before running the watch it'll print an error and return -1.
	 */
	public int getRunningTotalMillis() {

		switch (run){
		  
		  case 0:
			  //PApplet.print("StopWatch hasn't started yet");
			  return 0;
		  case 1:
			  return (int)(System.currentTimeMillis() - runningTotal);
		  case 2:
			  return (int)runningTotal;
		  }	
		return -1;
	}

	/**
	 * @return The Running Total of time in seconds as a float. The Running Total of time can only be reset with the start(). If you get the result before running the watch it'll print an error and return -1.
	 */
	public float getRunningTotalSeconds() {
		
		switch (run){
		  
		  case 0:
			  //PApplet.print("StopWatch hasn't started yet");
			  return 0;
		  case 1:
			  return (float)(System.currentTimeMillis() - runningTotal ) * 0.001f;
		  case 2:
			  return (float)runningTotal * 0.001f;
		  }	
		return -1;		
	}
}